//
//  ZHRainbowButton.m
//  ZHS_UCDemo
//
//  Created by Emrys on 2016/11/21.
//  Copyright © 2016年 Emrys. All rights reserved.
//

#import "ZHRainbowButton.h"

@interface ZHRainbowButton ()

@property(nonatomic, strong, readonly) UIBezierPath *bezierPath;

@end

@implementation ZHRainbowButton

- (void)setHighlighted:(BOOL)highlighted {} // 取消高亮状态

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.imageView.frame = self.bounds;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    
    if ([super pointInside:point withEvent:event]) {
        
        if ([self.bezierPath containsPoint:point]) {
            //如果在path区域内，返回YES
            return YES;
        }
        return NO;
    }
    return NO;
}

- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    shapeLayer.path = self.bezierPath.CGPath;
    
    self.layer.mask = shapeLayer;
    
    self.layer.masksToBounds = YES;
    
    [self clipsToBounds];
}

- (UIBezierPath *)bezierPath {
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];

    CGFloat radius = self.frame.size.height;
    
    [bezierPath addArcWithCenter:CGPointMake(radius, radius)
                          radius:radius
                      startAngle:M_PI
                        endAngle:M_PI*(1+self.progress)
                       clockwise:YES];

    [bezierPath addLineToPoint:CGPointMake(radius, radius)];
    [bezierPath closePath];
    
    return bezierPath;
}

@end
